package labox.innovation.gameserver.model;

import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastList;
import labox.innovation.config.Config;
import labox.innovation.db.PlayFriends;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * @author ljsh
 * @date 2010-4-12 
 * @version 1.0
 * @description 好友管理类
 */
public class FriendInstance {
	private static Logger _log=LoggerFactory.getLogger(FriendInstance.class);
	private FuzePcInstance owner;

	private FastList<FriendInfo> _friendList;

	private int _isInvited;// 是否关闭组队邀请

	private int province = -1;// 省

	private int city = -1;// 市

	private int isLoginTip;// 好友上线提示 0开或1关

	private FastList<FriendsNews> _friendsNews;;

	private L2FriendsNewsManager newsManager;

	/**
	 * 加我为好友的人的ID
	 */
	private List<Long> heroesId = new LinkedList<Long>();

	public FriendInstance(FuzePcInstance owner) {
		this.owner = owner;
		_friendList = new FastList<FriendInfo>();
		_friendsNews = new FastList<FriendsNews>();
		newsManager = L2FriendsNewsManager.getInstance();
	}

	public L2FriendsNewsManager getNewsManager() {
		return newsManager;
	}

	public List<Long> getHeroesId() {
		return heroesId;
	}

	public void addheroesId(Long id) {
		heroesId.add(id);
	}

	public void clearheroesId() {
		heroesId.clear();
	}

	public int getProvince() {
		return province;
	}

	public void setProvince(int province) {
		this.province = province;
	}

	public int getCity() {
		return city;
	}

	public void setCity(int city) {
		this.city = city;
	}

	public int getIsLoginTip() {
		return isLoginTip;
	}

	public void setIsLoginTip(int isLoginTip) {
		this.isLoginTip = isLoginTip;
	}

	public FuzePcInstance getOwner() {
		return owner;
	}

	public void setOwner(FuzePcInstance owner) {
		this.owner = owner;
	}

	/**
	 * @return 获取是否关闭组队邀请0是开启，1是关闭
	 */
	public boolean getIsInvited() {
		return _isInvited == 0 ? true : false;
	}

	/**
	 * @param isInvited
	 *            设置是否关闭组队邀请
	 */
	public void setIsInvited(boolean isInvited) {
		_isInvited = isInvited ? 0 : 1;
	}

	public FastList<FriendsNews> getFriendsNews() {
		return _friendsNews;
	}

	public FastList<FriendInfo> getFriendList() {
		return _friendList;
	}

	public void addFriend(FriendInfo friend) {
		if (!isContains(friend) && _friendList.size() < Config.FRIENDSLIMIT) {
			_friendList.add(friend);
		}
	}

	public void addFriend(FuzePcInstance friend) {
		if (!isContains(friend.getObjectId())) {
			FriendInfo info = new FriendInfo(owner.getObjectId(), friend.getObjectId(), friend.getLevel(), friend.getClassId().getId(), 0, friend.getName());
			addFriend(info);
		}
	}

	public boolean isContains(FriendInfo friend) {
		boolean flag = false;
		for (FriendInfo info : _friendList) {
			if (friend.getFriendId() == info.getFriendId())
				flag = true;

		}
		return flag;
	}

	public boolean isContains(long friendId) {
		boolean flag = false;
		for (FriendInfo info : _friendList) {
			if (friendId == info.getFriendId())
				flag = true;

		}
		return flag;
	}

	public FriendInfo getFriendInfo(long friendId) {
		FriendInfo fo = null;
		for (FriendInfo info : _friendList) {
			if (friendId == info.getFriendId()) {
				fo = info;
				break;
			}
		}
		return fo;
	}

	public void removrFriendInfo(long friendId) {
		FriendInfo fo = null;
		for (FriendInfo info : _friendList) {
			if (friendId == info.getFriendId()) {
				fo = info;
				break;
			}
		}
		_friendList.remove(fo);
	}

	public void addFriendNews(FriendsNews news) {
		if (_friendsNews.size() > Config.FRIENDSNEWSLIMIT) {
			_friendsNews.removeLast();
		}
		_friendsNews.addFirst(news);
	}

	/**
	 * 发送上线消息
	 */
	public void sendLoginMessage(){
		SystemMessage msg=new SystemMessage(SystemMessageId.YOUR_FRIEND_ONLINE);
		msg.addCharName(getOwner());
		FuzePcInstance player=null;
		for (Long objid : getHeroesId()) {
			player = FuzeWorld.getInstance().getPlayer(objid);
			if (player != null) {
				if (player.getFriendInstance().getIsLoginTip() == 0 ? true : false) {
					player.sendPacket(msg);
				}
			}
		}
	}
	
	/**
	 * 加载好友数据
	 */
	public void load(){
		PlayFriends.restoreFriendList(getFriendList(), getOwner());
	}
	/**
	 * 保存好友数据
	 */
	public void save(){
		try {
			PlayFriends.updateplayer_Friends(getOwner(),getFriendList());
		} catch (Exception e) {
			_log.error("update friends list error", getOwner().getName(), e);
		}
		
	}
}
